package StudyCode.summary.myfile;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DeleteTemporaryFileWithStandardOpenOptionDELETE_ON_CLOSE {
	public static void main(String[] args) throws Exception {
		Path basedir = FileSystems.getDefault().getPath("E:/Copy/");
		String tmp_file_prefix = "test_";
		String tmp_file_sufix = ".txt";
		Path tmp_file = null;
		try {
			tmp_file = Files.createTempFile(basedir, tmp_file_prefix, tmp_file_sufix);
		} catch (IOException e) {
			System.err.println(e);
		}
		try (OutputStream outputStream = Files.newOutputStream(tmp_file,
				StandardOpenOption.DELETE_ON_CLOSE);
		     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				     outputStream))) {
			Thread.sleep(1000);
			System.out.println("删除临时文件 " + tmp_file + "成功");
		} catch (IOException | InterruptedException e) {
			System.err.println(e);
		}
	}
}
